package edu.ustb.services.weatherforecast.utils;


import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.Properties;

/**
 * Sample: PropertiesUtil.getValue("filename.properties", "key")
 */

public class PropertiesUtil {
    private static Properties getProperties(String properties) {
        try {
            InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(properties);
            Properties p = new Properties();
            p.load(in);
            return p;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private static String getFromSLCFile(String SLCPath) throws IOException {
        String slc = null;
        try {
            ClassPathResource classPathResource = new ClassPathResource(SLCPath);
            InputStream inputStream = classPathResource.getInputStream();

            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            slc = result.toString();
            // System.out.println(slc);
            return slc;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String setProperties(String properties, String key, String value){
        Properties prop = new Properties();
        try{
            InputStream fis = PropertiesUtil.class.getClassLoader().getResourceAsStream(properties);
            // 从输入流中读取属性列表（键和元素对）
            prop.load(fis);
            // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
            OutputStream fos = new FileOutputStream("D:\\idea_workspace\\bus-and-gateway-test\\weather-forecast\\src\\main\\resources\\application-metadata.properties");

            // System.out.println(new File(properties).getAbsolutePath());
            prop.setProperty(key, value);
            // 以适合使用 load 方法加载到 Properties 表中的格式，
            // 将此 Properties 表中的属性列表（键和元素对）写入输出流
            prop.store(fos, "OK");

            return "OK";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getValue(String properties,String key){
        Properties p = getProperties(properties);
        return p.getProperty(key);
    }

    public static void init() throws IOException {
        String metaData = PropertiesUtil.getFromSLCFile("weatherforecast.slc");
        String key = "eureka.instance.metadata-map.my-metadata";

        PropertiesUtil.setProperties("application-metadata.properties", key, metaData);

        String res = PropertiesUtil.getValue("application-metadata.properties", key);
        // System.out.println(res);
    }

     /*public static void main(String[] args) throws IOException {
        String metaData = PropertiesUtil.getFromSLCFile("railwayticket.slc");
        String key = "eureka.instance.metadata-map.my-metadata";

        PropertiesUtil.setProperties("application-metadata.properties", key, metaData);

        String res = PropertiesUtil.getValue("application-metadata.properties", key);
        System.out.println(res);
    }*/
}


